﻿using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.com.ui.form;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.Com.UI.Interface;
using System;
using System.Windows.Forms;

namespace EkStudio.com.ui.unitbill.property
{
    public partial class ModuleUiJScriptProperty : DevExpress.XtraEditors.XtraUserControl
    {

        private BillUiJscriptToProperty bean = null;

        private BillFormatModule bill = null;

        private ProjectRootPanel RootPanel = null;

        private ProjectUnitBillInterface UnitPanel = null;

        private JScriptBindForm BindForm = null;

        public event KEventHandler CaptionChanged = null;

        public ModuleUiJScriptProperty(BillFormatModule _bean, ProjectRootPanel root, ProjectUnitBillInterface unit)
        {
            InitializeComponent();
            this.bill = _bean;
            this.RootPanel = root;
            this.UnitPanel = unit;
            this.InitPropery();
        }


        private void InitPropery()
        {
            Field_One_Caption.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_Two_Key.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_Three_EditType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_Four_Bind.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_Five_Content.Properties.ReadOnly = UnitPanel.ReadOnly;
            
        }

        public BillUiJscriptToProperty SelectField
        {
            set
            {
                bean = value;
                PropertyGridJs.SelectedObject = bean;
                Field_Five_Content.Visible = true;
            }
        }

        public BillUiJscriptToProperty[] SelectFields
        {
            set
            {
                bean = new BillUiJscriptToProperty(value);
                PropertyGridJs.SelectedObject = bean;
                Field_Five_Content.Visible = false;
            }
        }
        /// <summary>
        /// 初始Js内容
        /// </summary>
        private void InitJscript()
        {
            BillExpandJsCodeItem js = bean.JsItem;
            if(js.Content == "")
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("function ").Append(js.Key).Append("(){").AppendLine();
                sb.Append("\t").AppendLine().AppendLine();
                sb.Append("\treturn true;").AppendLine();
                sb.Append("}");
                js.Content = sb.ToString();
            }
        }



        /// <summary>
        /// 事件绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryBindEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (UnitPanel.ReadOnly) return;
            if(BindForm==null) BindForm = new JScriptBindForm(bill,UnitPanel);
            BindForm.SetInitRegEdit(bean.Bind);
            DialogResult DR = BindForm.ShowDialog();
            if(DR == DialogResult.OK)
            {
                bean.Bind = BindForm.RedEdit;
                UnitPanel.SaveStatus = false;
                PropertyGridJs.Refresh();
            }
        }
        /// <summary>
        /// 脚本编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryJscriptEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BillJscriptUnitPanel jsPanel = RootPanel.OpenJScriptPanel(UnitPanel, bill, bean.JsItem);
            UnitPanel.AddSubjectBill(jsPanel);
        }

        private void PropertyGridJs_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            UnitPanel.SaveStatus = false;
            if(e.Row.Properties.FieldName == "Caption")
            {
                if (this.CaptionChanged != null)
                {
                    PropertyDataEvent data = new PropertyDataEvent("", e.Value.ToString());
                    this.CaptionChanged(this, data);
                }
            }else if(e.Row.Properties.FieldName == "Key")
            {
                InitJscript();
            }
        }
    }
}
